package cron

import (
	"fmt"
	"github.com/robfig/cron/v3"
	"log"
	"yk-travel-social/common/model/mysql"
	"yk-travel-social/pkg"
)

func SyncEs() {
	c := cron.New(cron.WithSeconds())

	c.AddFunc("0 */10 * * * *", func() {

		s := mysql.ScenicSpot{}

		list, err := s.ShowList()
		if err != nil {
			log.Println("查询失败", err)
			return
		}
		fmt.Println(list)
		es, err := pkg.SyncEs(list)
		if err != nil {
			return
		}
		fmt.Println(es)

	})
	c.Start()
	select {}
}

func DedOrder(sn string, id, num, status int64) {
	c := cron.New(cron.WithSeconds())

	c.AddFunc("0 */30 * * * *", func() {

		o := mysql.Order{}
		orderSn, err := o.ShowOrderByOrderSn(sn)
		if err != nil {
			log.Println(err)
			return
		}
		fmt.Println(status, orderSn.OrderStatus)

		if orderSn.OrderStatus != int(status) {
			c.Stop()
		}

		err = o.UpdateOrderStatus(sn, "4")
		if err != nil {
			log.Println(err)
			return
		}

		s := mysql.ScenicSpot{}
		info, err := s.ShowInfo(id)
		if err != nil {
			return
		}
		Nums := info.Stock + int(num)
		fmt.Println(Nums)
		err = s.DedStock(id, Nums)
		if err != nil {
			log.Println(err)
			return
		}
	})
	c.Start()
	select {}
}
